import axios from '@/axios.config'

export default {
    state: {
        msg: '',
        roleList: [],
        totleNum: 0,
        verifyCode: 0,
        powerList: []
    },
    mutations: {
        send_msg(state, payload) {
            state.msg = payload._msg
        },
        add_roleList(state, payload) {
            state.roleList = [].concat(payload.com_rolelist);
            state.totleNum = payload.com_total
        },
        add_verify(state, payload) {
            state.verifyCode = payload.com_code
        },
        add_powerList(state, payload) {
            state.powerList = [].concat(payload.com_powerlist);
        }
    },
    actions: {
        //创建角色
        createRole({ commit }, payload) {
            return axios.post('/api/auth/role/save', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "保存成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //角色列表分页
        getRoleList({ commit }, payload) {
            return axios.post('/api/auth/role/list',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_roleList',{
                            com_rolelist: res.data.data.t,
                            com_total: res.data.data.totalCount
                        })
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //角色删除
        delRole({ commit }, payload) {
            return axios.post('/api/auth/role/del',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "删除成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //角色开启
        openRole({ commit }, payload) {
            return axios.post('/api/auth/role/open',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "开启成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //角色关闭
        closeRole({ commit }, payload) {
            return axios.post('/api/auth/role/close',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "关闭成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //角色所有列表
        getAllRoleList({ commit }, payload) {
            return axios.post('/api/auth/role/list',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_roleList',{
                            com_rolelist: res.data.data,
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //权限列表
        getPowerList({ commit }, payload) {
            return axios.post('/api/auth/config/items',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_powerList',{
                            com_powerlist: res.data.data,
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //保存权限列表
        savePowerList({ commit }, payload) {
            return axios.post('/api/auth/config/update',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "保存成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //立即调度
        refreshList({ commit }, payload) {
            return axios.post('/api/auth/role/refresh',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg',{
                            _msg: "调度成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
    }
}